package com.monitor.task.email;


import com.alibaba.fastjson.JSON;
import email.bean.NsEmail;
import email.client.NsEmailClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 发邮件工具类
 * Created by yanfenxing on 2018/8/11.
 */
public class SendEmail {

    private static Logger logger = LoggerFactory.getLogger(SendEmail.class);

    /**
     * 发送邮件工具类
     * @param mailContent 邮件内容
     * @param address 邮件组
     * @param subject  邮件主题
     */
    public static String sendEmail(String mailContent,String subject,String url) {
        logger.info("【发送邮件】 参数： url :{},subject:{}",url,subject);
        StringBuffer sb = new StringBuffer();
        sb.append(mailContent);
        String msgId = "APP_Notify_" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        NsEmail email = new NsEmail();
        email.setSubject(subject);
        email.setOrgId("0000115");
        email.setEmailType("02");
        email.setContent(mailContent);
        email.setTypeNo("0002");
        email.setInterfaceUrl(url);
        email.setFilePaths(new ArrayList<>());
        //邮件唯一单号
        email.setMsgId(msgId);
        logger.info("【发送邮件】 邮件参数:"+ JSON.toJSONString(email));
        try {
            email.setPreTimeDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            //02:成功，03：失败
            String result = NsEmailClient.sendmail(email).toString();
            //获取响应信息
            logger.info("【发送邮件】 响应： result = " +result);
            return  result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        sendEmail("你的账户余额低于配置余额<br>123123123","测试发邮件","http://10.100.140.37:8084/email/sendEmailNew.m");
    }
}
